define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'duty/index' + location.search,
                    add_url: 'duty/add',
                    edit_url: 'duty/edit',
                    del_url: 'duty/del',
                    multi_url: 'duty/multi',
                    import_url: 'duty/import',
                    table: 'duty',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'admin.nickname', title: '姓名'},
                        //{field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'position_id', title: __('Position'), searchList: {1:'家长',2:'班主任',3:'协管老师',4:'值日行政人员',5:'校车安全管理人员',6:'学校领导',}, formatter: Table.api.formatter.status},
                        {field: 'room.title', title: __('Room.title'), operate: 'LIKE'},
                        {field: 'circuit.title', title: __('Circuit.title'), operate: 'LIKE'},
                        {field: 'time', title: __('Time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            var options = table.bootstrapTable('getOptions');
            $("#btn-import-file_new").data("upload-success", function(data){
                Fast.api.ajax({
                    url: options.extend.import_url,
                    data: {file: data.url},
                }, function (data, ret) {

                    var $a = $("#cuowu");
                    if(ret.data != false)
                    {
                        $a.removeClass('hidden');
                        $a.attr("href", ret.data.file);
                        $a.attr("download", ret.data.filename);
                        //$("body").append($a);
                        $a[0].click();
                    }

                    //上传结果回调:刷新table数据
                    table.bootstrapTable('refresh');
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
